package BiShi.huawei;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Scanner;

public class Main2 {
    static int cnt = 0;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int n = sc.nextInt();
            if(n==0){
                System.out.println(0);
            }else{
                int[][] a = new int[n][];
                ArrayList<ArrayList<String>> arr = new ArrayList<>();
                for ( int i = 0; i < n;i++){
                    arr.add(new ArrayList<String>());
                    a[i] = new int[sc.nextInt() + 2];
                }
                for ( int i = 0; i < n;i++){
                    for(int j = 0; j < a[i].length; j++){
                        a[i][j] = sc.nextInt();
                    }
                }
                for ( int i = 0; i < a.length;i++){
                    helper(a,  0, i);
                }
                System.out.println(cnt);
                cnt = 0;
            }
        }

    }
    private static void helper(int[][] a, int res, int cur) {
        if(a[cur].length < 2){
            return;
        }
        if (a[cur].length==2) {
            res += a[cur][1];
            cnt = Math.max(cnt, res);
            return;
        }else{
            for( int i = 2; i < a[cur].length;i++){
                helper(a,  res+a[cur][1], a[cur][i]-1);
            }
        }
    }
}

